<template> 
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import echarts from "echarts";
require("echarts/theme/macarons"); // echarts theme
import resize from "./mixins/resize";
import { getBusinessOpportunities } from "@/api/admin_jsc";
const animationDuration = 3000;
const emphasisStyle = {
  itemStyle: {
    barBorderWidth: 1,
    shadowBlur: 10,
    shadowOffsetX: 0,
    shadowOffsetY: 0,
    shadowColor: "rgba(0,0,0,0.5)",
  },
};
export default {
  mixins: [resize],
  props: {
    listQuery: {
      type: Object,
    },
    className: {
      type: String,
      default: "chart",
    },
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "350px",
    },
    autoResize: {
      type: Boolean,
      default: true,
    },
  },
  data() {
    return {
      showbtns: false,
      chart: null,
      sourceArray: [],
      maxValue: 0,
      contractCountArray: [],
      dimensionsArray: [],
      yData3: [],
    };
  },

  mounted() {
    this.getList();
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    getList() {
      getBusinessOpportunities(this.listQuery).then((result) => {
        //维度
        this.dimensionsArray = ["regionName"];
        //各战区数据
        // var sourceArray = []
        // //战区最大值
        // var maxValue = 0;
        // //各战区合同数量
        // var contractCountArray = []
        let _self = this;
        let res = result.data;
        console.log(res.data.length);
        for (let i = 0; i < res.data.length; i++) {
          let contractCount = 0;
          var data = { regionName: res.data[i]["regionName"] };

          for (let j = 0; j < res.data[i].child.length; j++) {
            if (i == 0) {
              _self.dimensionsArray.push(
                res.data[i].child[j]["productTypeName"]
              );
            }

            //获取渠道名称
            data[res.data[i].child[j]["productTypeName"]] =
              res.data[i].child[j]["contractAmount"] == null
                ? 0
                : res.data[i].child[j]["contractAmount"];

            //计算所有战区产品类型合同最大值,以便控制柱状图最大值
            if (data[res.data[i].child[j]["productTypeName"]] > _self.maxValue)
              _self.maxValue = data[res.data[i].child[j]["productTypeName"]];

            //当前战区合同数量累加
            contractCount +=
              data[res.data[i].child[j]["contractCount"]] == null
                ? 0
                : data[res.data[i].child[j]["contractCount"]];
          }
          _self.sourceArray.push(data);
          _self.contractCountArray.push(contractCount);
        }
        

        this.$nextTick(() => {
          this.initChart();
        });
      });
    },
    initChart() {
      this.chart = echarts.init(this.$el, "macarons");
      this.chart.setOption({
        color: ["#FF89B0", "#FFCF5A", "#6EF3ED", "#AB84FF", "#52A8FE"],
        legend: {},
        tooltip: {},
        dataset: {
          dimensions: this.dimensionsArray,
          source: this.sourceArray,
        },
        xAxis: {
            name: "周", //坐标名字
            nameLocation: "end",//坐标位置，支持start,end，middle
           type: "category",
             axisTick:{
              show:false
            },
            
          axisLine: {
            lineStyle: {
              color: "#616266",
            },
          },
        },
          yAxis: [
          {
            type: "value",
             axisLine: {
                show:false,
              lineStyle: {
                color: "#616266",
              },
            },
            axisTick: {
              show: false,
            },
            name: "合同金额",
            position: "left",
             nameTextStyle:{
          color: "#616266",
               },
            min: 0,
            max: this.maxValue,
            splitLine: {
              //网格线
              show: false,
            },
            axisLabel: {
              formatter: "{value}",
              textStyle: {
                color: "#616266",
              },
            },
          },
          {
            type: "value",
            name: "合同数量",
             nameTextStyle:{
          color: "#616266",
               },
                  axisLine: {
                show:false,
              lineStyle: {
                color: "#616266",
              },
            },
             axisTick: {
              show: false,
            },
            min: 0,
            max: 1000,
            position: "right",
            splitLine: {
              //网格线
              show: false,
            },
            axisLabel: {
              formatter: "{value}",
              textStyle: {
                color: "#616266",
              },
            },
          },
        ],
        // Declare several bar series, each will be mapped
        // to a column of dataset.source by default.
        series: [
          { type: "bar" },
          { type: "bar" },
          { type: "bar" },
          { type: "bar" },
          {
            name: "合同数量",
            type: "line",
            yAxisIndex: 1,
            data: this.contractCountArray,
          },
        ],
      });
    },
  },
};
</script>
